﻿var Size = {
    version: '1.0.0.0.0',
    Shirts: 1,
    Shoes: 2,
    Trouser: 3,
    Tie: 4,
    Other: 5,

    ListByType: function () {
        
        $('#listSize tbody').html('<tr><td colspan="3" class="aligncenter"><img src="/images/loading.gif" alt=""/></td></tr>');
        
        var typeId;
        $.each($('#ddlType ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlType select.jqTransformHidden option:eq(' + index + ')').attr('value');
                typeId = value;
            }
        });

        var url = String.format('/GetRequest.ashx?m=size&action=listbytype&tId={0}&rand={1}', typeId, Math.random());
        $.getJSON(url, function (res) {
            if (res.Success) {
                if (res != null) {
                    $.Mustache.load('/templates/size/list.htm')
                    .done(function () {
                        $('#listSize tbody').html('');
                        $('#listSize tbody').mustache('tpl-size-list', res);
                        $('form').jqTransform();
                        $("#valid").validationEngine();
                        $('.topDir').tipsy({ fade: true, gravity: 's' });
                    });
                }
            }
        });
    },

    Create : function() {
        var name     = $('#listSize #txtName').val();
        var priority = $('#listSize #txtPriority').val();

        var url = String.format('/GetRequest.ashx?m=size&action=save&sId=0&name={0}&priority={1}', name, priority);
        $.post(url, function () {
            $.jGrowl('Tạo mới kích thước thành công!', { header: 'Thông báo:' });
            Size.ListAll();
        });
    },

    Save : function (id) {        
        var name     = $('#s-' + id).find('input:eq(0)').val();
        var priority = $('#s-' + id).find('input:eq(1)').val();
    
        var url = String.format('/GetRequest.ashx?m=size&action=save&sId={0}&name={1}&priority={2}', id, name, priority);
        $.post(url, function () {       
            $.jGrowl('Sửa kích thước thành công!', { header: 'Thông báo:' });
            Size.ListAll();
        });    
    },

    Delete : function (id) {
        $('#listSize table tr').removeClass('gradeC');
        $('#s-' + id).addClass('gradeC');

        jConfirm('', 'Bạn có chắc chắn muốn xóa?', function (result) {
            if (result) {
                var url = String.format('/GetRequest.ashx?m=size&action=delete&sId={0}', id);
                $.post(url, function () { })
                    .done(function () {
                        $('#toTop').click();
                        $.jGrowl(String.format('Xóa kích thước thành công!'), { header: 'Thông báo:' });
                        Size.ListAll();                        
                    });            
            }
        });
    }   
};